/**
 * Created Date: Monday, November 10th 2025, 11:11:18 pm
 * Author: CodingGorit
 * Contact: javafullstack2021@163.com
 * -----
 * Last Modified: Mon Nov 10 2025
 * Modified By: CodingGorit
 * -----
 * Copyright © 2019 —— 2025 fmin-courses.com All Rights Reserved
 * ------------------------------------
 * Javascript will save your soul!
 */


import { computed, Ref, ref } from 'vue'

export interface FileItem {
    id: number
    [key: string]: any
}

export function useFileSelection(items: Ref<FileItem[]>) {
    const selectedFiles = ref(new Set<number>())
    let lastSelectedId: number | null = null

    // 选中的文件列表
    const selectedList = computed(() => {
        return items.value.filter(item => selectedFiles.value.has(item.id))
    })

    // 选中的文件数量
    const selectedCount = computed(() => selectedFiles.value.size)

    // 是否全选
    const isAllSelected = computed(() => {
        return items.value.length > 0 && selectedFiles.value.size === items.value.length
    })

    // 是否部分选中（用于 indeterminate 状态）
    const isPartialSelected = computed(() => {
        return selectedCount.value > 0 && !isAllSelected.value
    })

    /**
     * 选中单个文件
     * @param id 文件 ID
     * @param shiftKey 是否按下 Shift 键（范围选择）
     * @param ctrlKey 是否按下 Ctrl/Cmd 键（多选）
     */
    const selectFile = (id: number, shiftKey = false, ctrlKey = false) => {
        const index = items.value.findIndex(item => item.id === id)
        if (index === -1) return

        if (shiftKey && lastSelectedId !== null) {
            // Shift + 点击：范围选择
            const lastIndex = items.value.findIndex(item => item.id === lastSelectedId)
            if (lastIndex !== -1) {
                const start = Math.min(index, lastIndex)
                const end = Math.max(index, lastIndex)
                for (let i = start; i <= end; i++) {
                    selectedFiles.value.add(items.value[i].id)
                }
            }
        } else if (ctrlKey) {
            // Ctrl/Cmd + 点击：多选
            if (selectedFiles.value.has(id)) {
                selectedFiles.value.delete(id)
            } else {
                selectedFiles.value.add(id)
            }
        } else {
            // 普通点击：单选
            selectedFiles.value.clear()
            selectedFiles.value.add(id)
        }

        lastSelectedId = id
    }

    /**
     * 切换文件选中状态
     * @param id 文件 ID
     */
    const toggleFile = (id: number) => {
        if (selectedFiles.value.has(id)) {
            selectedFiles.value.delete(id)
        } else {
            selectedFiles.value.add(id)
        }
        lastSelectedId = id
    }

    /**
     * 检查文件是否被选中
     */
    const isSelected = (id: number): boolean => {
        return selectedFiles.value.has(id)
    }

    /**
     * 全选所有文件
     */
    const selectAll = () => {
        selectedFiles.value.clear()
        items.value.forEach(item => {
            selectedFiles.value.add(item.id)
        })
    }

    /**
     * 取消全选
     */
    const deselectAll = () => {
        selectedFiles.value.clear()
        lastSelectedId = null
    }

    /**
     * 反向选择
     */
    const invertSelection = () => {
        const newSelected = new Set<number>()
        items.value.forEach(item => {
            if (!selectedFiles.value.has(item.id)) {
                newSelected.add(item.id)
            }
        })
        selectedFiles.value.clear()
        newSelected.forEach(id => selectedFiles.value.add(id))
    }

    /**
     * 删除选中的文件
     */
    const deleteSelected = () => {
        selectedFiles.value.clear()
        lastSelectedId = null
    }

    /**
     * 清空选择状态和最后选中的ID
     */
    const clear = () => {
        selectedFiles.value.clear()
        lastSelectedId = null
    }

    return {
        selectedFiles,
        selectedList,
        selectedCount,
        isAllSelected,
        isPartialSelected,
        selectFile,
        toggleFile,
        isSelected,
        selectAll,
        deselectAll,
        invertSelection,
        deleteSelected,
        clear
    }
}
